package com.xiangban.xiangbanmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.xiangbanmall.dao.MemberMapper;
import com.xiangban.xiangbanmall.dao.MemberRecommendMapper;
import com.xiangban.xiangbanmall.exception.BizException;
import com.xiangban.xiangbanmall.model.TUserMember;
import com.xiangban.xiangbanmall.model.TUserRecommend;
import com.xiangban.xiangbanmall.model.dto.UserRecommend;
import com.xiangban.xiangbanmall.model.res.ApiResultCode;
import com.xiangban.xiangbanmall.utils.AesUtil;
import com.xiangban.xiangbanmall.utils.CommonUtils;
import com.xiangban.xiangbanmall.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xiangban.xiangbanmall.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/11/3 11:00
 */
@Service
public class MemberRecommendService {
    private static final String PASSWORD ="xiangban";

    @Autowired
    private MemberRecommendMapper memberRecommendMapper;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private MemberMapper memberMapper;



    public Object getQRCode() throws Exception {
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        return AesUtil.encrypt(PASSWORD, commonUtils.getUserIdByToken());
    }


    public void bindRecommendUser(Map<String,String> map) throws Exception {
        if (BizEmpty(map.get("cipherText"))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        int userId = Integer.parseInt(commonUtils.getUserIdByToken());
        String decrypt = AesUtil.decrypt(PASSWORD, map.get("cipherText"));
        if (commonUtils.getUserIdByToken().equals(decrypt)){
            throw new BizException(ApiResultCode.RECOMMEND_USER_BIND_NOT_YOU_SELF);
        }
        //是否已绑定推荐人
        TUserRecommend tUserRecommend = memberRecommendMapper.selectIsRecommend(userId);
        if (tUserRecommend != null){
            throw new BizException(ApiResultCode.RECOMMEND_USER_BIND);
        }
        TUserRecommend userRecommend = new TUserRecommend();
        userRecommend.setUserId(userId);
        userRecommend.setUserIdRecommend(Integer.parseInt(decrypt));
        memberRecommendMapper.insertRecommend(userRecommend);
    }

    public Object getRecommendedRecord() {
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        HashMap<String, Object> map = new HashMap<>();
        int userId = Integer.parseInt(commonUtils.getUserIdByToken());
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<UserRecommend> userRecommends = memberRecommendMapper.selectRecommendRecordList(userId);
        TUserMember userMember = memberMapper.getUserMember(userId);
        map.put("people",userRecommends.size());
        map.put("money",userMember.getMoney());
        map.put("record",userRecommends);
        return map;
    }

    public Object getUserRecommended(TUserRecommend recommend) {
       return memberRecommendMapper.selectIsRecommend(recommend.getUserId());
    }
}
